package com.example.service.impl;

import com.example.bean.UserBean;
import com.example.dao.AdminDao;
import com.example.dao.StudentDao;
import com.example.dao.TeacherDao;
import com.example.mapper.AdminMapper;
import com.example.mapper.StudentMapper;
import com.example.mapper.TeacherMapper;
import com.example.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Stream;

@Service
public class UserServiceImpl implements UserService {

    public static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    //将DAO注入Service层
    @Resource
    private AdminMapper adminMapper;
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private TeacherMapper teacherMapper;

    @Autowired
    private AdminDao adminDao;
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private TeacherDao teacherDao;

    @Override
    public UserBean login(Long id, String password, String identity) {
        return switch (identity) {
            case "admin" -> adminDao.getLoginInfo(id, password);
            case "student" -> studentDao.login(id, password);
            case "teacher" -> teacherDao.getLoginInfo(id, password);
            default -> null;
        };
    }

    @Override
    public Long register(String name, String password, String phone, String email, String identity) {
        return switch (identity) {
            case "admin" -> adminDao.register(name, password, phone, email);
            case "student" -> studentDao.register(name, password, phone, email);
            case "teacher" -> teacherDao.register(name, password, phone, email);
            default -> null;
        };
    }

    @Override
    public boolean judgePassword(String password){
        return password.matches("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$");
    }

    @Override
    public boolean judgePhone(String phone){
        return phone.matches("[0-9]{11}");
    }

    @Override
    public boolean judgeEmail(String email){
        return email.matches(".*@[a-zA-Z0-9]+(.[a-zA-Z0-9]+)+");
    }

    /**
     * @param user_name: 用户名
     * @param tel:       电话
     * @param e_mail:    邮箱
     * @param identity:  身份
     * @return boolean
     * @description: 新增老师
     **/
    @Override
    public boolean add_teacher(String user_name, String tel, String e_mail, String identity) {
        switch (identity) {
            case "导师" -> {
                String user_id = teacherMapper.get_Max_id();
                System.out.println(user_id);
                System.out.println(Long.parseLong(user_id) + 1);
                if (user_id == null) { // 第一个导师
                    user_id = "10000000";
                } else {
                    user_id = String.valueOf(Long.parseLong(user_id) + 1);
                }
                return adminMapper.addTeacher(user_id, user_name, tel, e_mail);
            }
            default -> {
                return false;
            }
        }
    }


    /**
     * @return List<UserBean>
     * @description: 显示用户
     **/
    @Override
    public List<UserBean> display_users() {
        List<UserBean> students = studentMapper.find_all_students();
        students.stream().forEach(x -> x.setIdentity("学生"));
        List<UserBean> teachers = teacherMapper.find_all_teachers();
        teachers.stream().forEach(x -> x.setIdentity("老师"));
        return Stream.concat(students.stream(), teachers.stream()).toList();
    }

}